package org.liuyuantao.lucene.search.advance;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.liuyuantao.lucene.utils.IKAnalyzer5x;

/**
 * lucene过滤查询和排序查询
 */
public class SimpleAdvanceSearch {
    private static IndexReader reader = null;
    private Directory directory;

    public SimpleAdvanceSearch(Directory directory) {
        this.directory = directory;
    }

    private IndexSearcher getSearcher() {
        try {
            if (reader == null) {
                reader = DirectoryReader.open(this.directory);
            } else {
                DirectoryReader tr = DirectoryReader.openIfChanged((DirectoryReader) reader);
                if (tr != null) {
                    reader.close();
                    reader = tr;
                }
            }
            return new IndexSearcher(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 过滤查询
     *
     * @param filter
     */
    public void searcherByFilter(Query filter) {
        try {
            IndexSearcher searcher = getSearcher();
            TopDocs tds = null;
            tds = searcher.search(filter, 50);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            for (ScoreDoc sd : tds.scoreDocs) {
                Document d = searcher.doc(sd.doc);
                System.out.println(sd.doc + ":(" + sd.score + ")" +
                        "[" + d.get("filename") + "【" + d.get("path") + "】--->" +
                        d.get("size") + "-----" + sdf.format(new Date(Long.valueOf(d.get("date")))) + "]");
            }
        } catch (NumberFormatException | IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 排序查询
     *
     * @param queryStr
     * @param sort
     */
    public void searcherBySort(String queryStr, Sort sort) {
        try {
            IndexSearcher searcher = getSearcher();
            QueryParser parser = new QueryParser("content", new IKAnalyzer5x(true));
            Query query = parser.parse(queryStr);
            TopDocs tds = null;
            if (sort != null) {
                tds = searcher.search(query, 50, sort);
            } else {
                tds = searcher.search(query, 50);
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (ScoreDoc sd : tds.scoreDocs) {
                Document d = searcher.doc(sd.doc);
                System.out.println(sd.doc + ":(" + sd.score + ")" +
                        "[" + d.get("name") + "【" + d.get("path") + "】---" + d.get("score") + "--->" +
                        d.get("size") + "-----" + sdf.format(new Date(Long.valueOf(d.get("date")))) + "]");
            }
        } catch (NumberFormatException | ParseException | IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 普通查询
     *
     * @param queryStr
     */
    public void searcherByQuery(Query queryStr) {
        try {
            IndexSearcher searcher = getSearcher();
            TopDocs tds = null;
            tds = searcher.search(queryStr, 50);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            for (ScoreDoc sd : tds.scoreDocs) {
                Document d = searcher.doc(sd.doc);
                System.out.println(sd.doc + ":(" + sd.score + ")" +
                        "[" + d.get("filename") + "【" + d.get("path") + "】--->" +
                        d.get("size") + "-----" + sdf.format(new Date(Long.valueOf(d.get("date")))) + "]");
            }
        } catch (NumberFormatException | IOException e) {
            e.printStackTrace();
        }
    }

}
